﻿using System;
using Newtonsoft.Json;

namespace Ecobee.Net
{
	public class ScheduleConverter : JsonConverter
	{
		public override bool CanConvert(Type objectType)
		{
			bool returnValue = false;


			return returnValue;
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			object returnValue = null;

			if (objectType == typeof(Schedule))
			{
				if (reader.TokenType == JsonToken.StartArray)
				{
					int i = 0;
					returnValue = new Schedule();

					while (reader.TokenType != JsonToken.EndArray)
					{
						reader.Read();

						if (reader.Value != null)
						{
							ScheduleTime scheduleTime = new ScheduleTime()
							{
								ClimateReference = (string)reader.Value,
								Index = i
							};

							((Schedule)returnValue).ScheduleTimes.Add(scheduleTime);
							i++;
						}
					}
				}
			}

			return returnValue;
		}

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			//writer.WriteValue(((Schedule)value).ClimateName);
		}
	}
}
